package org.sith.taskrunner.temp.tasks.quadraticequation;

import org.sith.taskrunner.api.task.Task;
import org.sith.taskrunner.api.exceptions.TaskExecutionException;
import org.sith.taskrunner.api.exceptions.TaskInitException;

public class QuadraticEquationTask implements
        Task<QuadraticEquationInputData, QuadraticEquationResult> {

	private QuadraticEquationInputData inputData;

	@Override
	public void init(QuadraticEquationInputData inputData)
			throws TaskInitException {
		this.inputData = inputData;
	}

	@Override
	public QuadraticEquationResult executeTask() throws TaskExecutionException {
		Triple triple = inputData.getTriple();
		double x1;
		double x2;
		Pair result;
		double a = triple.getA();
		double b = triple.getB();
		double c = triple.getC();
		if (a != 0) {
			double discremenant = b * b - 4 * a * c;
			if (discremenant < 0) {
				throw new TaskExecutionException(
						"Discremenant less then zero, no decisions in the real domain of numbers");
			} else {
				x1 = (-b - Math.sqrt(discremenant)) / (2 * a);
				x2 = (-b + Math.sqrt(discremenant)) / (2 * a);
			}

		} else {
			x1 = -c / b;
			x2 = x1;
		}
		result = new Pair(x1, x2);
		return new QuadraticEquationResult(result);
	}
}
